import {
  hrmEmployeeQueryLoginEmployeeAPI,
  hrmEmployeeAllListAPI
} from '/src/api/hrm/employee.js'
import {
  hrmDeptQueryTreeListAPI
} from '/src/api/hrm/dept.js'

import {debounce} from 'throttle-debounce'
import {defineStore} from "pinia";

export const useHrmStore = defineStore('hrm', {
  state: () => ({
    hrmUserInfo: null, // 人资用户信息
    hrmShowType: 1, // 1 管理 2 员工
    hrmUserList: [], // 人力资源员工和部门
    hrmDeptList: []
  }),

  actions: {
    SET_HRMUSERINFO(hrmUserInfo) {
      this.hrmUserInfo = hrmUserInfo
    },
    Log(hrmUserInfo) {
      console.log("aa hrmUserInfoLog", hrmUserInfo)
      return hrmUserInfo
    },
    SET_HRMSHOWTYPE(hrmShowType) {
      this.hrmShowType = hrmShowType
    },
    SET_HRMUSERLIST(data) {
      this.hrmUserList = data
    },
    SET_HRMDEPTLIST(data) {
      this.hrmDeptList = data
    },
    // 获取人资用户信息
    GetHrmUserInfo() {
      return new Promise((resolve, reject) => {
        hrmEmployeeQueryLoginEmployeeAPI().then(res => {
          const resData = res.data || {}
          this.SET_HRMUSERINFO(resData)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    debounceGetHrmUserList: debounce(3000, () => {
      this.GetHrmUserList()
    }),

    // 管理后台员工列表
    GetHrmUserList() {
      return new Promise((resolve, reject) => {
        hrmEmployeeAllListAPI({
          pageType: 0
        }).then(res => {
          this.SET_HRMUSERLIST(res.data || [])
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    debounceGetHrmDeptList: debounce(3000, () => {
      this.GetHrmDeptList()
    }),

    // 管理后台部门列表
    GetHrmDeptList() {
      return new Promise((resolve, reject) => {
        hrmDeptQueryTreeListAPI({}).then(res => {
          this.SET_HRMDEPTLIST(res.data || [])
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
})
